﻿package smooth.core.object.interactiveLocalizationComponent {
	
	import app.core.action.RotatableScalable;
	import flash.events.MouseEvent;
	import flash.events.TouchEvent;
	import flash.geom.Point;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	import flash.text.AntiAliasType;
	import flash.display.Shape;
	import caurina.transitions.Tweener;	
	import smooth.core.object.flickrComponent.FlickrComponent;
	import smooth.core.object.googleMapCanvasComponent.GoogleMapCanvasComponent;
	import smooth.core.object.googleMapComponent.GoogleMapComponent;
	import smooth.core.object.youTubeComponent.YouTubeComponent;
	import flash.display.Bitmap;
	import flash.display.Loader;
	import flash.events.Event;
	import flash.net.URLRequest;

	public class InteractiveLocalizationComponent extends RotatableScalable {
		
		[Embed(source="Arial.ttf", fontFamily="myFont")] 
		public var myFont:Class;
		
		public var initialPosX:int;
		public var initialPosY:int;
		public var t:TextField;
		private var lat:Number;
		private var lng:Number;
		private var text:String;
		private var clickgrabber:Shape = new Shape();	
		
		public function InteractiveLocalizationComponent(lat:Number, lng:Number, text:String):void {
			this.lat = lat;
			this.lng = lng;
			this.text = text;
			// Init des visuels du composant			
			initGraphics();
			// Init des events du composant
			initEvents();	
			// Init des process du composant
			initProcess();
		}
		
		
		private function initGraphics():void {
			clickgrabber.graphics.lineStyle(4,0xFFFFFF, 1);
			clickgrabber.graphics.beginFill(0xFFFFFF, 0.5);
			clickgrabber.graphics.drawCircle(0, 0, 45);
			clickgrabber.graphics.endFill();
			this.addChild(clickgrabber);
				
			var format:TextFormat= new TextFormat();
			format.font= "myFont";
			format.color= 0xFFFFFF;
			format.size = 30;
						
			this.t = new TextField();
			this.t.background = false;	
			this.t.alpha = 1;
			this.t.embedFonts = true;
			this.t.selectable  = false;		 
			this.t.text = this.text;
			this.t.autoSize = TextFieldAutoSize.LEFT;
			this.t.x = 50;
			this.t.y = -15;
			this.t.setTextFormat(format);
			this.addChild(this.t);
			var imageLoader:Loader =new Loader();
			imageLoader.contentLoaderInfo.addEventListener(Event.COMPLETE,earthIconLoadComplete);
			imageLoader.load(new URLRequest("../resources/web.png"));
		}
		
		private function initProcess():void {
		}
		
		private function initEvents():void {
			this.addEventListener(TouchEvent.MOUSE_UP, this.doTouchUp);
			this.addEventListener(MouseEvent.MOUSE_UP, this.doMouseUp);
		}
		
		private function earthIconLoadComplete(event:Event):void {
			var earthIcon:Bitmap = new Bitmap();
			earthIcon.bitmapData = event.target.content.bitmapData;
			earthIcon.alpha = 0.5;
			earthIcon.width = 80;
			earthIcon.height = 80;
			earthIcon.x = -40;
			earthIcon.y = -40;
			earthIcon.alpha = 0.8;
			this.addChild(earthIcon);			
		}
		
		private function doMouseUp(e:MouseEvent):void {
			var point:Point = new Point(e.stageX, e.stageY);
			doProcessMouseUp(point);
		}
		
		private function doTouchUp(e:TouchEvent):void {
			var point:Point = new Point(e.stageX, e.stageY);
			doProcessMouseUp(point);
		}
  
		private function doProcessMouseUp(point:Point):void {
			var actionsToProcess:Array = new Array();
			var targetObjects:Array = this.stage.getObjectsUnderPoint(point);
			actionsToProcess.unshift({objectType:"", targetObject:null})
			for (var i:int = 0; i < targetObjects.length;++i) {
				var targetObject:Object = targetObjects[i];
				if (targetObject is FlickrComponent) {
					actionsToProcess.unshift({objectType:"FlickrComponent", targetObject:targetObject});
				} else if (targetObject is GoogleMapCanvasComponent) {
					actionsToProcess.unshift({objectType:"GoogleMapCanvasComponent", targetObject:targetObject});
				} else if (targetObject is GoogleMapComponent) {
					actionsToProcess.unshift({objectType:"GoogleMapComponent", targetObject:targetObject});
				} else if (targetObject is YouTubeComponent) {
					actionsToProcess.unshift({objectType:"YouTubeComponent", targetObject:targetObject});
				}
			}
			
			if (actionsToProcess[0].objectType == "FlickrComponent") {
				actionsToProcess[0].targetObject.geoSearchService(this, 1);
				Tweener.addTween(this, { alpha:0, scaleX: 0.0, scaleY: 0.0, time:0.25, transition:"easeinoutquad" } );	
			} else if (actionsToProcess[0].objectType == "GoogleMapCanvasComponent") {
				actionsToProcess[0].targetObject.addMarkerService(this);
				Tweener.addTween(this, { alpha:0, scaleX: 0.0, scaleY: 0.0, time:0.25, transition:"easeinoutquad" } );	
			} else if (actionsToProcess[0].objectType == "GoogleMapComponent") {
				actionsToProcess[0].targetObject.addMarkerService(this);
				Tweener.addTween(this, { alpha:0, scaleX: 0.0, scaleY: 0.0, time:0.25, transition:"easeinoutquad" } );	
			} else if (actionsToProcess[0].objectType == "YouTubeComponent") {
				actionsToProcess[0].targetObject.geoSearchService(this, 1);
				Tweener.addTween(this, { alpha:0, scaleX: 0.0, scaleY: 0.0, time:0.25, transition:"easeinoutquad" } );	
			}
						
			Tweener.addTween(this, {alpha:1,scaleX: 1.0, scaleY: 1.0, x:this.initialPosX, y:this.initialPosY, time:0.25, transition:"easeinoutquad"});	
		}
		
		public function getLat():Number {
			return this.lat;
		}
		
		public function getLng():Number {
			return this.lng;
		}
		
		public function getText():String {
			return this.text;
		}
	}
	
}